using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryUI : MonoBehaviour
{
    public static InventoryUI Instance { get; private set; }

    private GameObject uiGameObject;
    private GameObject content;
    public Inventory inventory;
    public GameObject itemPrefab;

    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
        }

        Instance = this;
    }

    private void Start()
    {
        uiGameObject = transform.Find("UI").gameObject;
        content = transform.Find("UI/InventoryBg/Scroll View/Viewport/Content").gameObject;
        InitInv();
    }

    public void Show()
    {
        uiGameObject.SetActive(true);
    }

    public void Hide()
    {
        uiGameObject.SetActive(false);
    }

    public void InitInv()
    {
        for (int i = 0; i < content.transform.childCount; i++) {//清空节点下的子节点
            Destroy (content.transform.GetChild (i).gameObject);
        }

        int num = inventory.itemList.Count;
        for (int j = 0; j < num; j++)
        {
            if (inventory.itemList[j].itemSO.itemHeld > 0)
            {
                GameObject itemGo = GameObject.Instantiate(itemPrefab);
                itemGo.transform.parent = content.transform;
                ItemUI itemUI = itemGo.GetComponent<ItemUI>();
                string type = null;
                switch (inventory.itemList[j].itemSO.itemType)
                {
                    case ItemType.Armor:
                        type = "护甲";
                        break;
                    case ItemType.Arrow:
                        type = "箭";
                        break;
                    case ItemType.Boot:
                        type = "鞋子";
                        break;
                    case ItemType.Bow:
                        type = "弓";
                        break;
                    case ItemType.Glove:
                        type = "手套";
                        break;
                    case ItemType.Helmet:
                        type = "头盔";
                        break;
                    case ItemType.Weapon:
                        type = "武器";
                        break;
                    case ItemType.Misc:
                        type = "杂项";
                        break;
                    case ItemType.Ring:
                        type = "戒指";
                        break;
                    case ItemType.Shield:
                        type = "盾牌";
                        break;
                }

                itemUI.InitItem(inventory.itemList[j].itemSO, inventory.itemList[j].itemSO.itemIcon,
                    inventory.itemList[j].itemSO.itemName,
                    type,
                    inventory.itemList[j].itemSO.itemInfo);
            }
        }
    }

    public void AddItem(ItemSO itemSO)
    {
        if (itemSO.itemHeld == 0) //物品不存在
        {
            itemSO.itemHeld++;
            GameObject itemGo = GameObject.Instantiate(itemPrefab);
            itemGo.transform.parent = content.transform;
            ItemUI itemUI = itemGo.GetComponent<ItemUI>();
            string type = null;
            switch (itemSO.itemType)
            {
                case ItemType.Armor:
                    type = "护甲";
                    break;
                case ItemType.Arrow:
                    type = "箭";
                    break;
                case ItemType.Boot:
                    type = "鞋子";
                    break;
                case ItemType.Bow:
                    type = "弓";
                    break;
                case ItemType.Glove:
                    type = "手套";
                    break;
                case ItemType.Helmet:
                    type = "头盔";
                    break;
                case ItemType.Weapon:
                    type = "武器";
                    break;
                case ItemType.Misc:
                    type = "杂项";
                    break;
                case ItemType.Ring:
                    type = "戒指";
                    break;
                case ItemType.Shield:
                    type = "盾牌";
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            itemUI.InitItem(itemSO, itemSO.itemIcon, itemSO.itemName, type, itemSO.itemInfo);
        }
        else //已经存在则加一
        {
            itemSO.itemHeld++;
            InitInv();
        }
    }

    public void SearchItem(string itemName)
    {
        for (int i = 0; i < content.transform.childCount; i++)
        {
            if (content.transform.GetChild(i).GetComponent<ItemUI>().itemNameText.text.Contains(itemName))
            {
                content.transform.GetChild(i).gameObject.SetActive(true);
            }
            else
            {
                content.transform.GetChild(i).gameObject.SetActive(false);
            }
        }
    }
}